<?php

class Core_Model_Design_Collection_Page_Position extends Dcmp_Model_Collection_Strong {

    public function __construct() {
        parent::__construct();
        $this->setItemClass('design/page_position');
        $this->from('{t}design_page_position', 'a')->field('a.*');
    }

    public function joinTemplate(){
        return $this->leftJoin('{t}design_page_template', 'b', 'b.id', 'a.page_template_id')
                    ->field('b.name', 'template_name');
    }

    public function sortBy($name, $dir){
        switch($name){
            case 'id':
                return $dir == 'desc'? $this->desc('a.id'): $this->asc('a.id');
            case 'code':
                return $dir == 'desc'? $this->desc('a.code'): $this->asc('a.code');
            case 'name':
                return $dir == 'desc'? $this->desc('a.name'): $this->asc('a.name');
            case 'templateid':
                return $dir == 'desc'? $this->desc('a.page_template_id'): $this->asc('a.templateid');
        }
        return parent::sortBy($name, $dir);
    }

    public function filterBy($name, $arg) {
        switch ($name) {
            case 'id':
                return $this->addParameter('id', $arg)->eq('a.id', ':id:s');
            case 'templateid':
                return $this->addParameter('templateId', $arg)->eq('a.page_template_id', ':templateId:s');
            case 'themeid':
                return $this->addParameter('themeId', $arg)->eq('a.page_theme_id', ':themeId:s');
            case 'areaid':
                return $this->addParameter('areaId', $arg)->eq('a.area_id', ':areaId:s');
            case 'enabled':
                return $this->addParameter('enabled', $arg)->eq('a.enabled', ':enabled:i');
        }
        return parent::filterBy($name, $arg);
    }
}